#博客详细页自动化测试
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from BologAutoTest.Common.Utils import BlogDriver


class BlogDetail:
    url=""
    driver = ""
    def __init__(self):
        # 测试前要确保这个地址的博客存在
        self.url="http://8.137.19.140:9090/blog_detail.html?blogId=142873"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
#登录状态
    def BlogDetailSuc(self):
        # 检查博客的标题是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
        # 检查发布博客的时间是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
        # 检查用户基本信息的昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
            # 检查用户基本信息的文章是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查用户基本信息的分类是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")

        # 检查编辑按钮是否存在
        if self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)"):
            print("是本人发布的文章,有编辑按钮")
            sleep(1)
            # 截图--1
            BlogDriver.getScreeShot()
            self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)").click()
            sleep(1)
            # 截图--2
            BlogDriver.getScreeShot()
            self.driver.back()

        else:
            print("不是本人发布的文章,无编辑按钮")

        # 检查删除按钮是否存在
        if self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)"):
            print("是本人发布的文章,有删除按钮")
            sleep(1)
            # 截图--3
            BlogDriver.getScreeShot()
            self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
            wait = WebDriverWait(self.driver, 2)
            wait.until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            print(alert.text)
            assert alert.text == "确定删除?"
            # 关闭弹窗
            alert.accept()

        else:
            print("不是本人发布的文章,无删除按钮")

        sleep(1)
        #截图哦按段是否删除
        # 截图--4
        BlogDriver.getScreeShot()

        # 点击主页并截图
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)").click()
        sleep(2)
        # 截图--5
        BlogDriver.getScreeShot()
        # 返回上一页
        self.driver.back()

        # 点击写博客并截图
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()
        sleep(5)
        # 截图--6
        BlogDriver.getScreeShot()
        #返回上一页
        self.driver.back()


#未登录状态

    def ListNotLogin(self):
        # 点击注销并截图
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        # 截图--1
        BlogDriver.getScreeShot()
        sleep(1)

        self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=142873")
        # 截图--2
        BlogDriver.getScreeShot()
        try:
            WebDriverWait(self.driver,10).until(
                EC.presence_of_element_located(
                    (By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
                )
            )
            print("页面加载完成，找到了指定元素")
        except TimeoutException:
            print("超时：页面未加载完成或未找到指定元素")
